package com.freestyle.jmssample.rabbitmq;

import com.freestyle.jms.common.EhcacheUtil;
import com.freestyle.jmssample.api.JMSDeclare;
import com.freestyle.jmssample.api.SMSConstants;
import com.freestyle.jmssample.api.pojo.SMSBean;
import org.springframework.beans.factory.InitializingBean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.Resource;
import java.util.concurrent.ConcurrentHashMap;

/**
 * SMS 配置类
 * Created by rocklee on 2022/1/6 15:28
 */
@Configuration("smsConfig")
public class SMSConfig implements InitializingBean {
  @Resource
  private EhcacheUtil ehcacheUtil;
  @Resource
  private JMSDeclare jmsDeclare; //这里实例是RabbitMQDeclare

  @Override
  public void afterPropertiesSet() throws Exception {
    //如果交换机不存在，则定义
    if (!jmsDeclare.isExChangeExists(SMSConstants.SMS_EXCHANGE_DIRECT)){
        jmsDeclare.declareExchange(jmsDeclare.newDirectExchange(SMSConstants.SMS_EXCHANGE_DIRECT));
    }
    //如果交换机不存在，则定义
    if (!jmsDeclare.isExChangeExists(SMSConstants.SMS_EXCHANGE_TOPIC)){
      jmsDeclare.declareExchange(jmsDeclare.newTopicExchange(SMSConstants.SMS_EXCHANGE_TOPIC));
    }

    //如果队列不存在，则定义
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_DIRECT)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_DIRECT,false);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_DIRECT,SMSConstants.SMS_EXCHANGE_DIRECT,SMSConstants.SMS_KEY_DIRECT,null);
    }
    //如果队列不存在，则定义
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_DIRECT2)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_DIRECT2,false);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_DIRECT2,SMSConstants.SMS_EXCHANGE_DIRECT,SMSConstants.SMS_KEY_DIRECT,null);
    }
    //如果队列不存在，则定义
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_DIRECT3)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_DIRECT3);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_DIRECT3,SMSConstants.SMS_EXCHANGE_DIRECT,SMSConstants.SMS_KEY_DIRECT3,null);
    }
    //如果队列不存在，则定义
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_DIRECT4)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_DIRECT4);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_DIRECT4,SMSConstants.SMS_EXCHANGE_DIRECT,SMSConstants.SMS_KEY_DIRECT4,null);
    }
    //绑定topic(routerkey匹配)
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_TOPIC)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_TOPIC);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_TOPIC,SMSConstants.SMS_EXCHANGE_TOPIC,SMSConstants.SMS_KEY_TOPIC,null);
    }

    //如果交换机不存在，则定义
    if (!jmsDeclare.isExChangeExists(SMSConstants.SMS_EXCHANGE_FANOUT)){
      jmsDeclare.declareExchange(jmsDeclare.newFanoutExchange(SMSConstants.SMS_EXCHANGE_FANOUT));
    }
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_FANOUT_1)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_FANOUT_1);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_FANOUT_1,SMSConstants.SMS_EXCHANGE_FANOUT,"",null);
    }
    if (!jmsDeclare.isQueueExists(SMSConstants.SMS_QUEUE_FANOUT_2)){
      jmsDeclare.initQueue(SMSConstants.SMS_QUEUE_FANOUT_2);
      jmsDeclare.bindQueue(SMSConstants.SMS_QUEUE_FANOUT_2,SMSConstants.SMS_EXCHANGE_FANOUT,"",null);
    }
  }
  public void saveSMS(SMSBean sms,String id){
    ehcacheUtil.put("sms",id,sms,10);//10秒
  }
  public SMSBean getSMS(String id){
    return ehcacheUtil.get("sms",id);
  }
}
